package com.hb.unic.api.impl.service;

import com.hb.unic.api.audit.service.IAuditLogService;
import com.hb.unic.api.impl.mapper.IAuditLogMapper;
import com.hb.unic.core.base.PageParameter;
import com.hb.unic.core.base.PageResult;
import com.hb.unic.core.base.Result;
import com.hb.unic.core.log.UnicLogger;
import com.hb.unic.core.log.UnicLoggerFactory;
import com.hb.unic.model.dobj.AuditLogDO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 审核日志表服务层
 *
 * @version v0.1, 2023-09-02 00:13:43, create by huangbiao.
 */
@Service
public class AuditLogServiceImpl implements IAuditLogService {

    /**
     * log
     */
    private static final UnicLogger LOGGER = UnicLoggerFactory.getLogger(AuditLogServiceImpl.class);

    /**
     * 审核日志表数据库交互层
     */
    @Resource
    private IAuditLogMapper auditLogMapper;

    /**
     * 插入
     *
     * @param auditLog 插入的实体对象
     * @return 影响的行数
     */
    @Override
    public Result<Integer> insert(AuditLogDO auditLog) {
        return Result.success(auditLogMapper.insert(auditLog));
    }

    /**
     * 批量插入
     *
     * @param auditLogList 插入的实体对象列表
     * @return 影响的行数
     */
    @Override
    public Result<Integer> insertBatch(List<AuditLogDO> auditLogList) {
        return Result.success(auditLogMapper.insertBatch(auditLogList));
    }

    /**
     * 根据主键更新
     *
     * @param auditLog 更新的实体对象
     * @return 影响的行数
     */
    @Override
    public Result<Integer> updateById(AuditLogDO auditLog) {
        return Result.success(auditLogMapper.updateById(auditLog));
    }

    /**
     * 根据主键删除
     *
     * @param id 主键
     * @return 影响的行数
     */
    @Override
    public Result<Integer> deleteById(Long id) {
        Set<Long> ids = new HashSet<>();
        ids.add(id);
        return this.deleteByIds(ids);
    }

    /**
     * 根据主键集合批量删除
     *
     * @param ids 主键集合
     * @return 影响的行数
     */
    @Override
    public Result<Integer> deleteByIds(Set<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Result.success(0);
        }
        return Result.success(auditLogMapper.deleteByIds(ids));
    }

    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 结果
     */
    @Override
    public Result<AuditLogDO> selectById(Long id) {
        Set<Long> ids = new HashSet<>();
        ids.add(id);
        Result<List<AuditLogDO>> result = this.selectByIds(ids);
        return Result.success(result.getData().get(0));
    }

    /**
     * 根据主键集合批量查询
     *
     * @param ids 主键集合
     * @return 结果列表
     */
    @Override
    public Result<List<AuditLogDO>> selectByIds(Set<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Result.success(new ArrayList<>());
        }
        return Result.success(auditLogMapper.selectByIds(ids));
    }

    /**
     * 根据条件查询
     *
     * @param auditLog 查询条件
     * @return 结果列表
     */
    @Override
    public Result<List<AuditLogDO>> selectList(AuditLogDO auditLog) {
        return Result.success(auditLogMapper.selectList(auditLog));
    }

    /**
     * 分页条件查询
     *
     * @param auditLog 查询条件
     * @param pageNum  当前页
     * @param pageSize 每页条数
     * @return 结果列表
     */
    @Override
    public Result<PageResult<AuditLogDO>> selectPages(AuditLogDO auditLog, Integer pageNum, Integer pageSize) {
        long total = auditLogMapper.selectCount(auditLog);
        List<AuditLogDO> auditLogList = auditLogMapper.selectPages(auditLog, new PageParameter(pageNum, pageSize));
        return Result.success(new PageResult<>(auditLogList, total));
    }
}